package com.example.feifei.view.lear;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.feifei.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 心电图自定义View
 * Created by chenfeifei on 2018\7\19 0019.
 */

public class ECGView extends View {

    int horizontalBigGirdNum = 6;//横向的线，即纵向的格数
    int verticalBigGirdNum = 8;//纵向的线，即横向的格数
    //网格线的画笔
    Paint paint = new Paint();
    //心电图的画笔
    Paint etcPaint = new Paint();
    //心电图路径
    Path etcPath = new Path();

    int viewWidth;
    int viewHeight;
    int widthSmallGird;//每个最小宽度
    int heightSmallGird;
    int baseLine;//中心基线
    int maxlevel;//最大高度

    private List<Float> datas;

    public ECGView(Context context) {
        super(context);
        initData();
    }

    public ECGView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData();
    }
    private void initData() {
        datas = new ArrayList<>();
        //忘了个画笔
        paint.setStyle(Paint.Style.STROKE);
        //心电图画笔
        etcPaint.setColor(Color.RED);
        etcPaint.setStyle(Paint.Style.STROKE);
        etcPaint.setStrokeWidth(5);

        getETCdatas();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
        widthSmallGird = viewWidth/5;
        heightSmallGird = widthSmallGird*2/3;
        baseLine = viewHeight/2;
        maxlevel = viewHeight/3;

        getETCdatas();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawGirds(canvas);
        drawETCLine(canvas);
    }
    //画网格
    private void drawGirds(Canvas canvas) {
        //画纵线
        for(int i = 0;i<verticalBigGirdNum;i++){
            if(i%5==0){
                paint.setStrokeWidth(1);
            }else{
                paint.setStrokeWidth(3);
            }
            canvas.drawLine(i*widthSmallGird,0,i*widthSmallGird,viewHeight,paint);
        }
        //划横线
        for(int i=0;i<horizontalBigGirdNum*5;i++){
            if(i%5==0){
                paint.setStrokeWidth(3);
            }else{
                paint.setStrokeWidth(1 );
            }
            canvas.drawLine(0,i*heightSmallGird,viewWidth,i*heightSmallGird,paint);
        }
    }
    //画心电图线
    private void drawETCLine(Canvas canvas){
        canvas.drawPath(etcPath,etcPaint);
        etcPath.moveTo(0,baseLine - datas.get(0));
        for(int i = 0;i<datas.size();i++){
            float y = baseLine - datas.get(i);
            etcPath.lineTo(i*30,y);
        }
        canvas.drawPath(etcPath,etcPaint);

    }

    public void getETCdatas(){
//        for(int i = 0;i<6;i++){
//            datas.add(0f);
//        }
        for(int i=0;i<10;i++){
            double random;
            if(i%2==0){
                random = Math.random();
            }else{
                random = - Math.random();
            }
            float value = (float) (maxlevel*random);
            datas.add(value);
        }
        for(int i = 0;i<6;i++){
            datas.add(0f);
        }
    }
}
